/**
 * Project: apollo-base-tools
 * 
 * File Created at 2017年8月3日
 * 
 * Copyright 2015-2016 dx.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * DongXue software Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dx.com.
 */
package org.apollo.base.tools.ui.view;

import java.awt.Component;
import java.io.Serializable;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import org.apollo.base.tools.ui.constants.UIConstants;

/** 
* @ClassName: JListPanel 
* @Description:listPanel中展示带图标列表
* @author wuzhenfang(wzfbj2008@163.com)
* @date 2017年8月3日 上午10:32:25 
* @version V1.0 
*/
public class JListPanel extends JList<JLabel> {

	private static final long serialVersionUID = -5813711229498110085L;

	public JListPanel() {
		init();
	}

	private void init() {
		setCellRenderer(new DefaultListCellRenderer() {
			private static final long serialVersionUID = 1L;

			@Override
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
				setIcon(UIConstants.ICON_demo);
				setText(value.toString());

				if (isSelected) {
					setBackground(list.getSelectionBackground());
					setForeground(list.getSelectionForeground());
				} else {
					// 设置选取与取消选取的前景与背景颜色.
					setBackground(list.getBackground());
					setForeground(list.getForeground());
				}
				return this;
			}
		});
	}

}

class DefaultListCellRenderer extends JLabel  implements ListCellRenderer<Object>, Serializable{

	private static final long serialVersionUID = 3332378189553799788L;

	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		if (value instanceof Icon) {
			setIcon((Icon) value);
			setText("");
		} else {
			setIcon(null);
			setText((value == null) ? "" : value.toString());
		}
		return this;
	}
	
}